/**
 *
 */
define(function(){
    var ws_dolphin;
    //var m_ConnectorServer1;

    var Extra = function(project, params){
        this.project = project;
        this.name = 'ctiextra_dolphin';
        $SC.addEvtListener(this.name, "OnSCTIEvent", OnSCTIEvent, -1, params);
    };

    Extra.prototype = {
        queryCTICall : function(){

        }
    };

    function OnSCTIEvent(evt){
        var jsonobj = null;
        switch(evt.toLowerCase()){
            case "onlogin":
                openws_dophin();
                break;
            case "onlogout":
                closews_dolphin();
                break;
            case "oncallstart":
                jsonobj = {
                    cmd: 100,
                    parameters: {
                        from: "xxxx",
                        event: "onCall",
                        param: {userID: "262766", userName: "xxx", userSex: "0"}
                    }
                };
                sendnotify(jsonobj);
                break;
            case "oncallestablish":
                jsonobj = {
                    cmd: 100,
                    parameters: {
                        from: "xxxx",
                        event: "onTalkBegin",
                        param: {userID: "262766", userName: "xxx", userSex: "0"}
                    }
                };
                sendnotify(jsonobj);
                break;
            case "oncallend":
                jsonobj = {
                    cmd: 100,
                    parameters: {
                        from: "xxxx",
                        event: "onClosed",
                        param: {userID: "262766", userName: "xxx", userSex: "0"}
                    }
                };
                sendnotify(jsonobj);
                break;
        }
    };

    function openws_dophin() {
        closews_dolphin();

        for (let port = 12061; port < 12071; port++) {
            ws_dolphin = openws("ws://127.0.0.1:" + port, "wss_dolphin_api");
            if (ws_dolphin != null) {
                break;
            }
        }
        if (ws_dolphin == null) {
            logmsg("openws_dophin", "connect dolphin failed");
            $F.alert("连接多芬助手服务失败");
        } else {
            ws_dolphin.open = function() {
                logmsg("ws.open");
            };
            ws_dolphin.message = function(evt) {
                logmsg("ws.message", evt.data);
            };
            ws_dolphin.error = function() {
                logmsg("ws.error");
            };
            ws.close = function() {
                ws_dolphin = null;
                logmsg("ws.close");
            }
        }
    };

    function closews_dolphin() {
        try {
            if (ws_dolphin && ws_dolphin.readyState == 1) {
                logmsg("closews_dolphin");
                ws_dolphin.close();
            }
        } catch (e) {
            logmsg("closews_dolphin", e.message, e.stack);
        }
        ws_dolphin = null;
    }

    function openws(url, protocol) {
        try {
            logmsg("new WebSocket", url, protocol);
            return new WebSocket(url, protocol);
        } catch (e) {
            logmsg("openws", e.message);
            return null;
        }
    };

    function sendnotify(jsonobj) {
        logmsg("send", jsonobj);
        try {
            ws_dolphin.send(JSON.stringify(jsonobj));
        } catch (e) {
            logmsg("sendnotify", e.message, e.stack);
        }
    }

    function logmsg() {
        try {
            $SC.log("[ctiextra_dolphin]", arguments);
        } catch (e) {
            console.log("[ctiextra_dolphin]", arguments);
        }
    }

    return Extra;
});

